package rollercoaster.superfonta;



import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import rollercoaster.superfonta.math.SFMatrix4f;
import rollercoaster.superfonta.math.SFVertex3f;
import rollercoaster.superfonta.math.SFVertex4f;



//questa �� quella fissa che guarda dal lato (esterno)

public class Camera3 implements iCameraState,KeyListener{
	
	private float x = 5;
	private float y = 0;
	private float z = 0;
	
	private SFVertex4f focus4 = new SFVertex4f(x,y,z,1);
	private SFVertex4f target4 = new SFVertex4f(0,0,0,1);
	private SFVertex4f dir4 = new SFVertex4f();
	
	private SFVertex3f left;
	private SFVertex3f up;
	private float upL = 1;
	private float leftL = 1;
	
	
	
	

	

	public float getUpL() {
		return upL;
	}

	public void setUpL(float upL) {
		this.upL = upL;
	}

	public float getLeftL() {
		return leftL;
	}

	public void setLeftL(float leftL) {
		this.leftL = leftL;
	}

	public void setCameraMode(SFCamera camera, SFMatrix4f cameraMatrix) {
		SFVertex4f focusn = cameraMatrix.multMoP(focus4,true);
		SFVertex4f targetn = cameraMatrix.multMoP(target4, true);
		
		dir4 = new SFVertex4f(targetn.getX()-focusn.getX(),targetn.getY()-focusn.getY(),targetn.getZ()-focusn.getZ(), 1);
		
		SFVertex3f projectedTarget = dir4.cloneV();
		
		if(Math.abs(projectedTarget.getX())<0.00001f && Math.abs(projectedTarget.getZ())<0.00001f){ // se siamo sul piano YZ
			projectedTarget.setX(0);
			projectedTarget.normalize3f();
			
			left = new SFVertex3f(1, 0, 0);
			up = projectedTarget.cross(left);
			left = dir4.cross(up);		
			//left.mult(-1);
		}else{
			
			projectedTarget.setY(0);
			projectedTarget.normalize3f();
			up = new SFVertex3f(0, 1, 0);
			left = projectedTarget.cross(up);
			
			up = left.cross(dir4);
			//up.mult(-1);
			
			
		}
		
		left.normalize3f();
		dir4.normalize3f();
		up.normalize3f();
		
		camera.setF(focusn);
		camera.setUp(up);
		camera.setLeft(left);
		camera.setDir(dir4);
		camera.setUpL(upL);
		camera.setLeftL(leftL);
		camera.update();
		
	}

	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyCode()==KeyEvent.VK_LEFT){
			
			this.x = x + 0.2f;
			focus4.setX(x);
		}if(arg0.getKeyCode()==KeyEvent.VK_RIGHT){
			
			this.x = x - 0.2f;
			focus4.setX(x);
		}
		
		if(arg0.getKeyCode()==KeyEvent.VK_UP){
		
			this.y = y + 0.2f;
			focus4.setY(y);
		
		}if(arg0.getKeyCode()==KeyEvent.VK_DOWN){
		
			this.y = y - 0.2f;
			focus4.setY(y);
		
		}
		
		if(arg0.getKeyCode()==KeyEvent.VK_W){
			
			this.z = z + 0.2f;
			focus4.setZ(z);
		}if(arg0.getKeyCode()==KeyEvent.VK_S){
			
			this.z -= 0.2f;
			focus4.setZ(z);
		}if(arg0.getKeyCode() == KeyEvent.VK_0){
			
			upL = upL * 2;
			leftL = leftL * 2;
			setUpL(upL);
			setLeftL(leftL);
		}if(arg0.getKeyCode() == KeyEvent.VK_9){
			
			upL = upL / 2;
			leftL = leftL / 2;
			setUpL(upL);
			setLeftL(leftL);
		}
		
	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	



}
